/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mangaviewer.http.client;

import com.mangaviewer.json.IOMangaJsonFile;
import com.mangaviewer.model.Manga;
import com.mangaviewer.util.SearchConstants;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author dorian
 */
public class SearchHttpClient {
  public static List<Manga> fetchMangaListFromServer() throws IOException {
    Logger.getLogger(SearchHttpClient.class.getName()).log(Level.INFO, "fetching manga list from server...");
    
    List<Manga> manga = new ArrayList<Manga>();
    
    // Initializing client and request
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(SearchConstants.MANGA_EDEN_MANGA_URL);
    
    // Fetching response
    HttpResponse response = client.execute(request);
    
    Logger.getLogger(SearchHttpClient.class.getName()).log(Level.INFO, "Response code: " + response.getStatusLine().getStatusCode());
    
    // Reading response content
    InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
    BufferedReader br = new BufferedReader(isr);
    
    StringBuilder result = new StringBuilder();
    String line = "";
    while((line = br.readLine()) != null) {
      result.append(line);
    }
    
    String resultString = result.toString();
    
    // parsing result string
    return IOMangaJsonFile.parse(resultString);
  }
}
